# -*- coding: utf-8 -*-
import re
import logging

import scrapy
from scrapy import Selector

from moose_poetry_spider.items import MoosePoetryAuthorInfoItem

'''
诗人信息 + 详情地址
'''


class MoosePoetryAuthorInfoSpider(scrapy.Spider):
    name = 'MoosePoetryAuthorInfoSpider'

    logger = logging.getLogger(__name__)

    allowed_domains = ['www.shicimingju.com']

    start_urls = ['https://www.shicimingju.com/category/all']

    prefix_url = "https://www.shicimingju.com"

    def parse(self, response):
        pattern = re.compile(r'<[^>]+>', re.S)
        poetry_list = response.xpath('//div[@id="main_left"]/div[@class="card zuozhe_card"]').extract()
        if len(poetry_list) > 0:
            author_info_item = MoosePoetryAuthorInfoItem()
            for poetry_item_str in poetry_list:
                html_introduction = Selector(text=poetry_item_str).xpath(
                    '//div[@class="zuozhe_list_item"]/div[@class="zuozhe_list_des"]').extract_first()
                if html_introduction is not None:
                    introduction = pattern.sub("", html_introduction).replace(" ", "").replace("\n", "")
                    author_info_item['introduction'] = introduction

                magnum_opus_html = Selector(text=poetry_item_str).xpath(
                    '//div[@class="zuozhe_good_shici_div"]').extract_first()
                if magnum_opus_html is not None:
                    magnum_opus = pattern.sub("", magnum_opus_html).replace(" ", "").replace("\n", "")
                    author_info_item['magnum_opus'] = magnum_opus

                author_info = Selector(text=poetry_item_str).xpath(
                    '//div[@class="zuozhe_list_item"]/h3/a/text()').extract_first()
                # self.logger.info("author_info %s", author_info)
                dynasty = re.findall(r'\[.*]', author_info)
                if len(dynasty) == 1:
                    dynasty = dynasty[0]
                    if dynasty is not None:
                        author_info_item['dynasty'] = dynasty

                author_name = re.findall(r'\[.*](.*)', author_info)
                # self.logger.info("author_name %s", author_name)
                if len(author_name) == 1:
                    author_name = author_name[0]
                    if author_name is not None:
                        author_info_item['author_name'] = author_name

                author_detail_url = Selector(text=poetry_item_str).xpath(
                    '//div[@class="zuozhe_list_item"]/h3/a/@href').extract_first()
                if author_detail_url is not None:
                    author_detail_url = str.format("{}{}", self.prefix_url, author_detail_url)
                    author_info_item['detail_url'] = author_detail_url
                    yield author_info_item

        # 抓取下一页数据
        next_url = response.xpath(
            "//div[@id='main_left']/div[@id='list_nav']/div[@id='list_nav_part']/a[text()='下一页']/@href").extract_first()
        if next_url is not None:
            next_page_url = str.format("{}{}", self.prefix_url, next_url)
            yield scrapy.Request(next_page_url, callback=self.parse, dont_filter=True)
